package com.smartown.androiddevelopart;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.os.Process;
import android.util.Log;

import java.util.Iterator;

/**
 * Created by Smartown on 2017/2/8.
 * <p>
 * <a href="https://github.com/smartown">https://github.com/smartown<a/>
 */
public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        String processName = getProcessName(this, Process.myPid());
        Log.i("MyApplication", "Start MyApplication in process " + processName);
    }

    public static String getProcessName(Context context, int pid) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
        Iterator iterator = activityManager.getRunningAppProcesses().iterator();
        while (iterator.hasNext()) {
            ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo) (iterator.next());
            if (info.pid == pid) {
                return info.processName;
            }
        }
        return "";
    }

}
